import { observable, action } from 'mobx';
import { WeaForm } from 'comsMobx';
import { message } from 'antd';
import { WeaLocaleProvider } from 'ecCom';
import * as Api from '../../apis/setting/mailSetting';
import { ValidateForm } from './util';

const getLabel = WeaLocaleProvider.getLabel;
const initalState = {
  fields: [],
};
export default class ConventionalStore {
  @observable conventionalForm = new WeaForm();
  @observable fields = initalState.fields;

  @action setStoreState = (params) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
  }

  @action clear = () => this.setStoreState(initalState);

  @action getEmailSettingCondition = (params) => {
    Api.getEmailSettingCondition(params).then((res) => {
      this.conventionalForm.initFormFields(res.conditioninfo);
      this.fields = res.conditioninfo;
    });
  }

  @action operateUserEmailSetting = (params) => {
    ValidateForm(this.conventionalForm, () => {
      let formParams = this.conventionalForm.getFormParams();
        params = { ...formParams, ...params };
        Api.operateUserEmailSetting(params).then((res) => {
          if (res.flag) {
            message.success(getLabel(384443, '更新设置成功'));
          } else {
            message.error(getLabel(384444, '更新设置失败'));
          }
        });
    });
  }
}
